package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.FriendsMapper;
import com.zy.imagineCommunity.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendsService {

    @Autowired
    FriendsMapper friendsMapper;

    public void addFriends(Friends friends) {
        friendsMapper.addFriends(friends);
    }

    //根据类型、起始条数、每页个数查询好友
    public Page<FriendsText> queryFriendsByParentIdAndStatusAndPageNoAndPageSize(int parentId, int pageNo, int pageSize) {
        Page<FriendsText> page = new Page<FriendsText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = friendsMapper.queryFriendsCountTotalByParentIdAndStatus(parentId, 1);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<FriendsText> FriendsList = friendsMapper.queryFriendsByParentIdAndStatusAndBeginAndPageSize(parentId, 1, begin, pageSize);
        //设置每页内容
        page.setItems(FriendsList);
        return page;
    }

    //根据类型、起始条数、每页个数查询申请
    public Page<FriendsText> queryApplyByUserIdAndStatusAndPageNoAndPageSize(int userId, int pageNo, int pageSize) {
        Page<FriendsText> page = new Page<FriendsText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = friendsMapper.queryApplyCountTotalByUserIdAndStatus(userId, 0);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<FriendsText> FriendsList = friendsMapper.queryApplyByUserIdAndStatusAndBeginAndPageSize(userId, 0, begin, pageSize);
        //设置每页内容
        page.setItems(FriendsList);
        return page;
    }

    //通过id删除申请、好友
    public void deleteFriendsOrApplyById(int id) {
        friendsMapper.deleteFriendsOrApplyById(id);
    }

    //通过id查找好友
    public Friends queryFriendsById(int id) {
        return friendsMapper.queryFriendsById(id);
    }

    //通过id修改好友状态
    public void updateFriendsById(Friends friends) {
        friendsMapper.updateFriendsById(friends);
    }

    public Friends queryFriendsByUserIdAndParentId(int userId, int parentId) {
        return friendsMapper.queryFriendsByUserIdAndParentId(userId, parentId);
    }

    /**
     * 通过用户id和父id删除好友
     *
     * @param userId
     * @param parentId
     */
    public void deleteFriend(int userId, int parentId) {
        friendsMapper.deleteFriend(userId, parentId);
    }

    /**
     * 搜索好友
     *
     * @param parentId
     * @param pageNo
     * @param searchValue
     * @param searchValue
     * @return
     */
    public Page<FriendsText> searchValueFriendsByParentIdAndStatusAndPageNoAndPageSizeAndSearchValue(int parentId, int pageNo, int pageSize, String searchValue) {
        Page<FriendsText> page = new Page<FriendsText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = friendsMapper.queryFriendsCountTotalByParentIdAndStatusAndSearchValue(parentId, 1, searchValue);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<FriendsText> FriendsList = friendsMapper.queryFriendsByParentIdAndStatusAndBeginAndPageSizeAndSearchValue(parentId, 1, begin, pageSize, searchValue);
        //设置每页内容
        page.setItems(FriendsList);
        return page;
    }

    /**
     * 根据userId和状态查询总记录数
     *
     * @param userId
     * @param status
     * @return
     */
    public int queryApplyCountTotalByUserIdAndStatus(int userId, int status) {
        return friendsMapper.queryApplyCountTotalByUserIdAndStatus(userId, status);
    }

}
